namespace CSSharpPro._1023;

public class Demo
{
    public void TestPrint()
    {
        int[] arr1 = new int[5];
        arr1[0] = 1;
        arr1[1] = 4;
        for (int i = 0; i < arr1.Length; i++)
        {
            Console.Write(i+"\t");
        }
        Console.WriteLine();
        
        String [] arr2 = new string [5];
        arr2[0] = "hello";
        arr2[4] = "world";
        for (int i = 0; i < arr1.Length; i++)
        {
            Console.Write(arr2[i]+"\t");
        }
        int []arr3 = {1,1,2,3,4};
        Console.WriteLine();
    }
     //冒泡排序
    public void TestPrint2(int[]arr)
    {
        
        if (arr == null || arr.Length <= 1)
            return;

        int n = arr.Length;
        bool swapped;

        // 外层循环控制轮次
        for (int i = 0; i < n - 1; i++)
        {
            swapped = false;

            // 内层循环比较相邻元素
            for (int j = 0; j < n - i - 1; j++)
            {
                if (arr[j] > arr[j + 1])
                {
                    // 交换元素
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;

                    swapped = true; // 标记发生交换
                }
            }

            // 若本轮无交换，数组已有序，提前退出
            if (!swapped)
                break;
        }
    }
    public void PrintArray(int[] arr)
    {
        if (arr == null)
        {
            Console.WriteLine("数组为空");
            return;
        }

        foreach (int num in arr)
        {
            Console.Write(num + " ");
        }
        Console.WriteLine();
    }

    public void TestPrint3()
    {
        
    }
}
